<?php
// +----------------------------------------------------------------------
// | NewThink [ Think More,Think Better! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +----------------------------------------------------------------------
// | 版权所有：山西创泰网络科技有限公司
// +----------------------------------------------------------------------
// | Author: 燕南天 QQ:81020302 Date:2018-08-01 Time:16:23
// +----------------------------------------------------------------------

namespace app\api\controller;

use think\Controller;
use think\Loader;

abstract class Base extends Controller
{
    protected $isWechatBrowser;
    protected $isLogin;
    protected $isReg;
    protected $no_login;
    protected $saveLog;
    /**
     * 权限类初始化
     * Power by Mikkle
     * QQ:776329498
     */
    public function initialize()
    {
        parent::initialize();
        
        $user_agent = $this->request->server('HTTP_USER_AGENT');
        if (! strpos($user_agent, 'MicroMessenger') === false ) $this->isWechatBrowser = true;
        
        //判断提交方式和是否微信浏览器
        if ($this->request->method() == 'GET' && $this->isWechatBrowser === true){
            //未登录 重新登录
            if (!$this->checkAuth()&& !$this->no_login )  $this->wxoauth();
            $this->isLogin=true;
            //设置全局登录
            $this->loginGlobal();
            if(!$this->isReg){
                if(!$this->checkUuidMobile()) $this->redirect('user/user_blind.html');
            }
        }
        
    }
    
    /**
     * Power by Nick
     * QQ:81020302
     * @param string $code
     * @param string $msg
     * @param array $data
     * @return array
     */
    static public function showReturnCode($code = '', $msg = '', $data = [])
    {
        $return_data = [
            'code' => '500',
            'msg' => '未定义消息',
            'data' => $code == 1001 ? $data : [],
        ];
        if (empty($code)) return $return_data;
        $return_data['code'] = $code;
        if(!empty($msg)){
            $return_data['msg'] = $msg;
        }else if (isset(ReturnCode::$return_code[$code]) ) {
            $return_data['msg'] = ReturnCode::$return_code[$code];
        }
        return $return_data;
    }
    
    /**
     * 获取当前类名称
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param bool|false $all
     * @return string
     */
    public function getClassName($all = false)
    {
        return $all
            ? get_called_class()
            : basename(str_replace('\\', '/', get_called_class()),'.php');
    }
    
    /**
     * 数据库字段 网页字段转换
     * 标识为数据库字段 值为浏览器提交字段
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $array   标识为数据库字段 值为浏览器提交字段
     * @param bool|false $uuid  是否追加UUID信息
     * @return array
     */
    protected function buildParam($array,$uuid=false)
    {
        $data=[];
        foreach( $array as $item=>$value ){
            $data[$item] = $this->request->param($value);
        }
        if ($uuid && isset($this->uuid)){
            $data['uuid'] = $this->uuid;
        }
        return $data;
    }
    
    /**
     * 检测是否登录
     * Power by Mikkle
     * QQ:776329498
     * @return bool
     */
    public function checkLoginGlobal()
    {
        $check_success = false;
        switch ($this->loginType) {
            case 1;
            case "session";
                $this->uuid = Session::get('uuid', 'Global');
                $this->member_info = Session::get('member_info', 'Global');
                if ($this->uuid && $this->member_info) {
                    $check_success = true;
                }
                break;
            case 2;
            case "cache";
                $session_id_check = Cookie::get("session_id");
                $this->uuid = Cache::get("uuid_{$session_id_check}");
                $this->member_info = Cache::get("member_info_{$session_id_check}");
                if ($this->uuid && $this->member_info) {
                    $check_success = true;
                }
                //刷新 缓存有效期
                Cache::set("uuid_{$session_id_check}", $this->uuid);
                Cache::set("member_info_{$session_id_check}", $this->member_info);
                break;
            case 3:
            case "redis":
                
                //这部分不方便共享 不好意思
                break;
        }
        return $check_success;
        
    }
    
    /**
     * 设置全局登录
     * #User: Mikkle
     * #Email:776329498@qq.com
     * #Date:
     */
    public function setLoginGlobal($member_info = [], $login_code = 0)
    {
        $set_success = false ;
        if ($member_info) {
            switch ($this->loginType) {
                case 1:
                case "session":
                    Session::set('member_info', $member_info, 'Global');
                    Session::set('uuid', $member_info['uuid'], 'Global');
                    if ((Session::has("uuid", "Global"))) {
                        $set_success = true;
                    }
                    break;
                case 2:
                case "cache":
                    $session_id = $this->create_uuid("SN");
                    Cookie::set("session_id", $session_id);
                    Cache::set("member_info_$session_id", $member_info);
                    Cache::set("uuid_$session_id", $member_info['uuid']);
                    $session_id_check = Cookie::get("session_id");
                    if ((Cache::get("uuid_{$session_id_check}"))) {
                        $set_success = true;
                    }
                    break;
                case 3:case "redis":
                //这部分不方便共享 不好意思
                
                break;
                
            }
        }
        if (!$set_success) return false;
        //保存登录记录
        $this->saveLoginInfo($member_info['uuid'],$login_code);
        
        return true;
    }
    
    /**
     * 全局退出
     * Power by Mikkle
     * QQ:776329498
     * @return bool
     */
    protected function logoutGlobal()
    {
        switch ($this->loginType) {
            case 1:
            case "session":
                Session::delete('uuid', 'Global');
                Session::delete('member_info', 'Global');
                break;
            case 2:
            case "cache":
                $session_id_check = Cookie::get("session_id");
                Cache::rm("uuid_{$session_id_check}");
                Cache::rm("member_info_{$session_id_check}");
                Cookie::delete("session_id");
                break;
            case 3:case "redis":
            //这部分不方便共享 不好意思
            
            
            break;
        }
        $this->member_info = null;
        $this->uuid = null;
        return true;
    }
    
    /**
     * 快速调用执行model函数
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $param_data
     * @param bool|false $validate_name
     * @param bool|false $model_name
     * @param string $action_name
     * @return array
     */
    protected function doModelAction($param_data,$validate_name = false, $model_name = false,$action_name='editData'){
        
        if ($validate_name != false) {
            $result = $this->validate($param_data, $validate_name);
            if (true !== $result) return $this->showReturnCode(1003, [], $result);
        }
        $model_edit = Loader::model($model_name);
        if (!$model_edit) return $this->showReturnCode(1010);
        return $model_edit->$action_name($param_data);
    }
    
    /**
     * 快速修改
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param bool|false $parameter
     * @param bool|false $validate_name
     * @param bool|false $model_name
     * @param array $save_data
     * @return array
     */
    protected function editData($parameter = false, $validate_name = false, $model_name = false, $save_data = [])
    {
        if (empty($save_data)) {
            if ($parameter != false && is_array($parameter)) {
                $data = $this->buildParam($parameter);
            } else {
                $data = $this->request->post();
            }
        } else {
            $data = $save_data;
        }
        if (!$data) return $this->showReturnCode(1004);
        //追加登录UUID
        if ($this->checkLoginGlobal() && !isset($data['uuid'])) $data['uuid'] = $this->uuid;
        
        if ($validate_name != false) {
            $result = $this->validate($data, $validate_name);
            if (true !== $result) return $this->showReturnCodeWithOutData(1003,$result);
        }
        $model_edit = Loader::model($model_name);
        if (!$model_edit) return ['code' => 1010];
        return $model_edit->editData($data);
    }
}